﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using NHibernate;
using NHibernate.Context;
using NUnit.Framework;

namespace Revis.VClinicNet.Core.Domain.Persistence.Nh.Tests.Db
{
    public abstract class DatabaseTestBase
    {
        private ITestDatabase _database = null;

        protected ISessionFactory Factory
        {
            get { return _database.Factory; }
        }

        protected ISession Session
        {
            get { return _database.Factory.GetCurrentSession(); }
        }

        public DatabaseTestBase(ITestDatabase database)
        {
            _database = database;            
            _database.Configure();
            Console.WriteLine("Database configured");
        }

        [SetUp]
        public void SetUp()
        {
            _database.BindCurrentSessionContext();
            _database.ExportSchema();
            Console.WriteLine("------------------------------------");
            Console.WriteLine("Connection to empty database created");
        }

        [TearDown]
        public void TearDown()
        {
            _database.UnbindCurrentSessionContext();
            Console.WriteLine("Connection to database disposed");
            Console.WriteLine("------------------------------------");
        }

        public void InTransaction(Action<ISession> action)
        {
            using (ITransaction transaction = Session.BeginTransaction()) {
                action(Session);
                transaction.Commit();
            }
        }
    }
}
